package com.its.web;

import com.github.pagehelper.PageInfo;
import com.its.pojo.HupCompany;
import com.its.pojo.WorkingPersonnel;
import com.its.service.HupCompanyService;
import com.its.service.WorkingPersonnelService;
import com.its.util.HttpStatusEnum;
import com.its.util.Result;
import com.its.web.param.HupCompanyControllerParam;
import com.its.web.param.WorkingPersonnelControllerParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 工作人员信息表 前端控制器
 * </p>
 *
 * @author 朱厚强
 * @since 2023年12月27日
 */
@RestController
@RequestMapping("/workingPersonnel")
public class WorkingPersonnelController {
    @Autowired
    private WorkingPersonnelService workingPersonnelService;
    /**
     * 获取所有的员工信息
     * @return
     */
    @PostMapping("/pageList")
    public Result pagingList(@RequestBody WorkingPersonnelControllerParam param){
        PageInfo<WorkingPersonnel> pageInfo = workingPersonnelService.selectList(param);
        if (pageInfo.getSize() > 0){
            return Result.ok().data(pageInfo).code(HttpStatusEnum.SUCCESS.getCode()).message("查询所有员工信息成功");
        }else {
            return Result.error().data(pageInfo).code(HttpStatusEnum.ERROR.getCode()).message("查询所有员工信息失败");
        }
    }
    /**
     * 根据id获取员工信息
     * @return
     */
    @GetMapping("/getById/{id}")
    public Result getById(@PathVariable String id){
        WorkingPersonnel workingPersonnel = workingPersonnelService.getById(id);
        if (workingPersonnel != null){
            return Result.ok().data(workingPersonnel).code(HttpStatusEnum.SUCCESS.getCode()).message("查询员工信息成功");
        }else {
            return Result.error().data(workingPersonnel).code(HttpStatusEnum.ERROR.getCode()).message("查询员工信息失败");
        }
    }
    /**
     * 添加员工信息
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody WorkingPersonnel workingPersonnel){
        boolean save = workingPersonnelService.save(workingPersonnel);
        if (save){
            return Result.ok().code(HttpStatusEnum.SUCCESS.getCode()).message("员工信息添加成功");
        }else {
            return Result.error().code(HttpStatusEnum.ERROR.getCode()).message("员工信息添加失败");
        }
    }
    /**
     * 删除信息(逻辑删除)
     * @return
     */
    @DeleteMapping("/remove")
    public Result remove(@RequestParam Integer id ){
        boolean b = workingPersonnelService.updateById(new WorkingPersonnel().setId(id).setIsWork(1));
        if (b){
            return Result.ok().code(HttpStatusEnum.SUCCESS.getCode()).message("删除员工信息成功");
        }else {
            return Result.error().code(HttpStatusEnum.ERROR.getCode()).message("删除员工信息失败");
        }
    }
    /**
     * 更新数据
     */
    @PutMapping("/update")
    public Result update(@RequestBody WorkingPersonnel workingPersonnel){
        boolean b = workingPersonnelService.updateById(workingPersonnel);
        if (b){
            return Result.ok().code(HttpStatusEnum.SUCCESS.getCode()).message("修改公司信息成功");
        }else {
            return Result.error().code(HttpStatusEnum.ERROR.getCode()).message("修改公司信息失败");
        }
    }
}
